<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="util.*"%>
<%@ page import="domain.entities.interfaces.*"%>
<%@ page import="domain.dao.interfaces.*"%>
<%@ page import="org.springframework.context.ApplicationContext"%>
<%@ page import="org.springframework.context.support.ClassPathXmlApplicationContext"%>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%>
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
<%@ taglib prefix="nested" uri="http://struts.apache.org/tags-nested"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<link rel="stylesheet" type="text/css" href="../css/bootstrap.css">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Update contact</title>
<link rel="stylesheet" type="text/css" href="/mdoc-project/css/bootstrap_Boris.css">
<base href="<%=request.getScheme()%>://<%=request.getServerName()%>:<%=request.getServerPort()%><%=request.getContextPath()%>/">
</head>
<body>

	<%
		boolean connected = session
				.getAttribute(DomainUtils.CURRENT_USER_STRING) != null;

	
		ApplicationContext context = null;
		IDAOPhoneNumber daoPhoneNumber = null;
		IDAOContact daoContact = null;
		String strIdContact = null;
		int idContact = -1;
		IContact c = null;
		IAddress cAdd = null;
		boolean isEntreprise = false;

		if (connected == true) {
			context = util.SpringUtils.getApplicationContext();
			daoPhoneNumber = (IDAOPhoneNumber) context
					.getBean("beanDAOPhoneNumber");
			daoContact = (IDAOContact) context.getBean("beanDAOContact");
			strIdContact = request.getParameter("selected_contact");
			idContact = Integer.parseInt(strIdContact);
			c = daoContact.getById(idContact);
			cAdd = null;
			isEntreprise = false;
		}
		
		if (connected == false) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING,
					"You must be connected");
		} else if (c == null) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING,
					"This contact has been deleted.");
		} else {
			cAdd = c.getAdd();
			isEntreprise = (c instanceof IEntreprise);

			request.setAttribute("first_name", c.getFirstName());
			request.setAttribute("last_name", c.getLastName());
			request.setAttribute("e_mail", c.getEmail());
			request.setAttribute("street", cAdd.getStreet());
			request.setAttribute("city", cAdd.getCity());
			request.setAttribute("zip", cAdd.getZip());
			request.setAttribute("country", cAdd.getCountry());

			if (c instanceof IEntreprise) {
				IEntreprise tmp = (IEntreprise) c;
				request.setAttribute("num_siret", "" + tmp.getNumSiret());
			}

			for (IPhoneNumber phone : daoPhoneNumber.getByContactId(c
					.getId())) {

				if (phone.getPhoneKind().compareTo("Mobile") == 0) {
					request.setAttribute("mobile", phone.getPhoneNumber());
				} else if (phone.getPhoneKind().compareTo("House") == 0) {
					request.setAttribute("house", phone.getPhoneNumber());
				} else if (phone.getPhoneKind().compareTo("Work") == 0) {
					request.setAttribute("work", phone.getPhoneNumber());
				}
			}
		}
	%>

	<%@include file="/pages/common_style/header.jsp"%>
	<%@include file="/pages/common_style/alerts.jsp"%>

	<%
		if (connected && c != null) {
	%>
	<html:form action="/UpdateContact">
		<html:errors />
		<br>
		<div class="form-group">
			<label for="firstname">First name</label>
			<input type="text" class="form-control" id="firstname" value="<%=DomainUtils
							.getNotNullValue(request, "first_name")%>" placeholder="Enter a first name" name="first_name">
			<label for="lastname">Last name</label>
			<input type="text" class="form-control" id="lastname" value="<%=DomainUtils.getNotNullValue(request, "last_name")%>" placeholder="Enter a last name" name="last_name">
			<label for="email">E-mail</label>
			<input type="email" class="form-control" id="email" value="<%=DomainUtils.getNotNullValue(request, "e_mail")%>" placeholder="Enter an e-mail adress" name="e_mail">
			<label for="street">Street</label>
			<input type="text" class="form-control" id="street" value="<%=DomainUtils.getNotNullValue(request, "street")%>" placeholder="Enter a street number and street name " name="street">
			<label for="city">City</label>
			<input type="text" class="form-control" id="city" value="<%=DomainUtils.getNotNullValue(request, "city")%>" placeholder="Enter a city's name" name="city">
			<label for="zip">Zip code</label>
			<input type="text" class="form-control" id="zip" value="<%=DomainUtils.getNotNullValue(request, "zip")%>" placeholder="Enter a zip code " name="zip">
			<label for="country">Country</label>
			<input type="text" class="form-control" id="country" value="<%=DomainUtils.getNotNullValue(request, "country")%>" placeholder="Enter a country name " name="country">
			<label for="mobile">Mobile</label>
			<input type="text" class="form-control" id="mobile" value="<%=DomainUtils.getNotNullValue(request, "mobile")%>" placeholder="Enter a phone number" name="mobile">
			<label for="house">House</label>
			<input type="text" class="form-control" id="house" value="<%=DomainUtils.getNotNullValue(request, "house")%>" placeholder="Enter a phone number" name="house">
			<label for="work">Work</label>
			<input type="text" class="form-control" id="work" value="<%=DomainUtils.getNotNullValue(request, "work")%>" placeholder="Enter a phone number " name="work">
			<input type="hidden" name="selected_contact" value="<%=strIdContact%>">

			<%
				if (c != null) {
			%>
			<input type="hidden" name="selected_contact_version" value="<%=c.getVersion()%>" />
			<%
				}
			%>

			<%
				if (isEntreprise) {
			%>
			<label for="numsiret">Num Siret</label>
			<input type="text" class="form-control" id="siret" value="<%=DomainUtils.getNotNullValue(request,
								"num_siret")%>" placeholder="Enter a siret number" name="num_siret">
			<%
				}
			%>
			<br>
			<button type="submit" class="btn btn-default">Submit</button>
			<button type="reset" class="btn btn-default">Reset</button>
		</div>
	</html:form>
	
	<br>
	<a class="btn btn-default navbar-btn" href="pages/showAllContact.jsp">Return</a>
	<%
		}
	%>

	<%@include file="/pages/common_style/footer.jsp"%>
</body>
</html>